package cn.tony.game.super_tank;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

import cn.tony.game.super_tank.msg.JoinMessage;
import cn.tony.game.super_tank.msg.Message;
import cn.tony.game.super_tank.msg.Messages;

public class GameServer {

    public static void main(String[] args) throws IOException {

        //
        // Game game = new Game();
        // game.start();
        // game.addself(true);

        GameServer g = new GameServer();
        g.start();
    }

    public static AtomicInteger idx = new AtomicInteger(1);

    Map<Integer, Socket>        mm  = new ConcurrentHashMap();

    int                         dir = 0b0010;
    int                         x   = 50;
    int                         y   = 50;

    boolean                     lr  = true;

    public void start() throws IOException {
        ServerSocket ss = new ServerSocket(8888);

        while (true) {
            Socket socket = ss.accept();
            System.out.println("join ...");
            int id = idx.incrementAndGet();

            InputStream is = socket.getInputStream();
            DataInputStream dis = new DataInputStream(is);

            String name = dis.readUTF();
            System.out.println(name + " join ");

            if (!lr) {
                dir = 0b1000;
                x = Game.W - x;
                y += 50;
            }
            lr = !lr;

            JoinMessage joinMessage = new JoinMessage();
            joinMessage.id = id;
            joinMessage.x = x;
            joinMessage.y = y;
            joinMessage.dir = dir;
            joinMessage.name = name;
            joinMessage.currentBlood=100;

            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
            dos.writeInt(id);
            dos.flush();

            mm.put(id, socket);

            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        Messages msg = new Messages();
                        try {

                            Message message = msg.relove(new DataInputStream(socket.getInputStream()));
                            if (message == null) {
                                continue;
                            }
                            message.reslove(dis);
                            sendAll(message, id, true);

                        } catch (IOException e) {
                            e.printStackTrace();
                            try {
                                socket.close();
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                            mm.remove(id);
                            return;
                        }
                    }
                }
            }).start();
            sendAll(joinMessage, id, false);
            System.out.println("join ... over");
        }

    }

    public void sendAll(Message message, int id, boolean isex) throws IOException {
        for (Iterator iterator = mm.keySet().iterator(); iterator.hasNext();) {
            Integer xx = (Integer) iterator.next();
            if (xx == id && isex) {
                continue;
            }
            try {
            message.send(mm.get(xx));
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
